<?php
/**
 * Author: yixuan
 * Desc:易宣系统全局变量处理文件
 * require_path: 挂载文件路径  application/common.php:19
 * Time: 2016/5/18 17:31
 */

//接收参数
use think\Db;


global $_G, $_P;
$_P = \think\Request::instance()->param();
//处理参数$_G相关逻辑
$_G['ispost'] = \think\Request::instance()->isPost();
$_G['isajax'] = \think\Request::instance()->isAjax();
$_G['url_scheme'] = $_SERVER['SERVER_PORT'] == 443 || (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') || strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https' || strtolower($_SERVER['HTTP_X_CLIENT_SCHEME']) == 'https' ? 'https://' : 'http://';//协议类型
$_G['clientip'] = \app\lib\IpLocation::getip();//ip
$_G['address'] = \app\lib\IpLocation::getLocation($_G['clientip']);//ip位置
$_G['url_domain'] = $_G['siteroot'] = $_G['url_scheme'] . $_SERVER['HTTP_HOST'] . "/";//网站URL根目录
$_G['url_full'] = $_G['url_scheme'] . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];//当前网址

//临时变量
//
//$_G['session_id'] = 'pkdknxraspyfmp3242yetviaxyrpq1ph';
//$_SESSION['token'][$_G['token']] = 1;


$_P['do'] = lcfirst(\think\Request::instance()->controller());

//获取模块信息
$server_confg_t = Db::name('00serverconfig')->find();
$_G['app_web_wxapp'] = \think\Request::instance()->module();

if ($_P['do'] == 'payment') {
    $tmp_do = \think\Request::instance()->action();
    if ($tmp_do == 'notify') {
        $tmp_xml = file_get_contents('php://input');
        $simpleXml = simplexml_load_string($tmp_xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $tmp_notify_data = json_decode(json_encode((array)$simpleXml), true);
        if ($tmp_notify_data && $tmp_notify_data['attach']) {
            $tmp_format_attach = explode('-', $tmp_notify_data['attach']);
            $_P['m'] = $tmp_format_attach[0];
            $_P['themeid'] = $tmp_format_attach[1];
        }
    } elseif ($tmp_do == 'pay') {
        $decode_sl = json_decode(base64_decode($_P['sl']), true);
        if ($decode_sl['module']) {
            $_P['m'] = $decode_sl['module'];
        }
        if ($decode_sl['themeid']) {
            $_P['themeid'] = $decode_sl['themeid'];
        }
    }
}
//初始化系统全局变量
require_once APP_PATH . 'common/decrypt.php';
if (!empty($_P['pdata'])) {
    $str = $_P['pdata'];
    $key = substr(md5(date("dmY")), 2, 14);
    $decrypt = wxapp_decrypt($str, $key);

    $_P['pdata'] = 1;
    if ($decrypt) {
        $decrypt = json_decode($decrypt, true);
        $_P = array_merge($_P, $decrypt);

        // $_POST = ihtmlspecialchars($_POST);
    } else {  //解密错误
        http_response_code(501);
        exit(json_encode(['code' => 501, 'msg' => '数据错误']));
    }
}
if ($_G['app_web_wxapp'] == 'wxapp') {
    $_G['mpid'] = empty($_P['i']) ? $_P['mpid'] : $_P['i'];
}

$action = \think\Request::instance()->action();

$now_ac = strtolower($_P['do'] . '/' . $action);
$eroor_msg = '';
$ac_white_list = array(
    'savelog/savelog',
    'entry/index',
    'entry/uploads',
    'wupload/w_upload',
    'openid/oauth',
    'entry/oauth',
    'payment/notify',
    'payment/pay',
    'wxappauth/index',
    'oaflow/index',
    'llsmscallback/llsmscallback',
);
if ($_P['do'] != 'oa' && !in_array($now_ac, $ac_white_list)) {
    if (!empty($_P['m'])) {
        !defined('MODULE_NAME') && define('MODULE_NAME', $_P['m']);
        $Condition = array('module_name' => $_P['m']);
        if ($_G['app_web_wxapp'] == 'web' && $_P['do'] == 'theme' && $_P['op'] == 'post') {
            $_P['themeid'] = $_P['id'];
        } else {
            $_P['themeid'] = isset($_P['themeid']) ? $_P['themeid'] : 0;
        }

        if (!empty($_P['themeid']) && $_P['themeid'] != -1) {
            $Condition['themeid'] = $_P['themeid'];
            if ($_G['app_web_wxapp'] != 'web' && $_P['do'] != 'excel' && $_P['do'] != 'payment') {
                $_P['soft'] = isset($_P['soft']) ? $_P['soft'] : '';
                if (($_P['soft'] || $_P['softkey']) && $_P['soft'] !== 'NULL' && $_P['softkey'] !== 'NULL') {
                    $Condition['softkey'] = $_P['soft'] ? $_P['soft'] : $_P['softkey'];
                }
                $module = Db::table('yx_sys_modules')->where($Condition)->find();
            } elseif (!empty($_P['themeid'])) {
                $module = Db::table('yx_sys_modules')->where($Condition)->find();
            }
            if (empty($module)) {
                $eroor_msg = '活动参数错误1:' . $now_ac;
            }
        } else if ($_G['app_web_wxapp'] == 'web') {
            $sql = "SELECT * FROM `yx_sys_modules` where module_name ='" . $_P['m'] . "' and (theme->>'$.status' <> '-1' || theme->>'$.id' is null) ORDER BY id desc";
            $module = Db::query($sql)[0];
            if (empty($module)) {
                $sql = "show tables like '" . config('database.prefix') . $_P['m'] . '_atheme' . "'";
                $isTableExists = DB::query($sql);
                if ($isTableExists) {
                    $module_themeid = Db::table('yx_sys_modules')->where(array('module_name' => $_P['m']))->order('id', 'desc')->find();
                    $themeid = empty($module_themeid) ? 1 : $module_themeid['themeid'] + 1;
                    $module = [
                        'module_name' => $_P['m'],
                        'themeid' => $themeid,
                        'mpid' => 0,
                    ];
                    Db::table('yx_sys_modules')->insert($module);
                } else {
                    $eroor_msg = '活动参数错误2:' . $now_ac;
                }
            }
        }
        $_G['system'] = empty($module['system']) ? array() : json_decode($module['system'], true);
        if ($_G['app_web_wxapp'] == 'wxapp') {
            $_G['mpid'] = empty($module['system']['wxapp']) ? $_G['mpid'] : $module['system']['wxapp'];
        } else {
            $_G['mpid'] = empty($module['mpid']) ? 0 : $module['mpid'];
        }
        $_P['themeid'] = empty($module['themeid']) ? 1 : $module['themeid'];
        if (!empty($_G['system'])) {
            $_G['theme'] = Db::name($_P['m'] . '_atheme')->where(['id' => $_P['themeid']])->find();
            $settings = Db::name($_P['m'] . '_aconfig')->where(['`key`' => $_P['themeid']])->find();
            if (is_array($settings)) {
                $_G['config'] = unserialize($settings['value']);
            } else {
                $_G['config'] = array();
            }
        } else {
            $sql = "show tables like '" . config('database.prefix') . $_P['m'] . '_atheme' . "'";
            $isTableExists = DB::query($sql);
            if (!$isTableExists) {
                $eroor_msg = '活动参数错误3:' . $now_ac;
            }
        }
        unset($module);
//    $_G['theme'] = empty($module['theme']) ? array() : json_decode($module['theme'], true);
//    $_G['config'] = empty($module['config']) ? array() : json_decode($module['config'], true);


    } else {
        if ($_G['app_web_wxapp'] == 'web') {
            if (!in_array(strtolower($_P['do']), ['platform', 'yxmodule', 'users', 'installupdate', 'login', 'single'])) {
                $eroor_msg = '活动参数错误4:' . $now_ac;
            }
        } else {
            $eroor_msg = '活动参数错误5:' . $now_ac;
        }
    }
}

if (!empty($eroor_msg)) {
    echo $eroor_msg;
    $url = $_G['url_domain'];
    echo "<script language='javascript' type='text/javascript'>";
    echo "window.location.href='$url';";
    echo "</script>";
    exit;
}
!defined('MODULE_NAME') && define('MODULE_NAME', $_P['m']);
//获取服务器配置
$server_confg_t = Db::name('00serverconfig')->find();
$server_confg = empty($server_confg_t) ? array() : json_decode($server_confg_t['config'], true);
if (empty($server_confg['domain0'])) {
    die('服务器参数错误：00serverconfig为空');
}
$_G['companyname'] = $server_confg['companyname']; //公司名称
$_G['companyname_short'] = mb_substr($server_confg['companyname'], 0, 2); //公司简称
$_G['system']['s'] = $server_confg; //临时将所有的参数$system
$_G['WebTitle'] = explode('.', $_G['system']['s']['domain0'])[0];
$_G['WebTitle'] .= $server_confg['companyname'] == '易宣软件' ? '易宣软件 【易宣·微信营销专家】' : $server_confg['companyname'];
if ($_G['app_web_wxapp'] == 'web') {
    if ($_G['companyname'] == '易宣软件') {
        $_G['companylogo'] = 'http://img2020.yixuansoft.cn/images/global/DM2z8Y78x7mKD8HUYK98T8dX52DDYm.png';
    } elseif ($_G['companyname'] == '飞驰软件') {
        $_G['companylogo'] = 'http://cdn.xjpf.net/images/global/oElBbLff55yxlbXfb1L19C57fCFpLB.png';
    } elseif ($_G['companyname'] == '联登科技') {
        $_G['companylogo'] = 'http://liandeng.jiemeo.com/images/global/higZ52uIVGY1GLiSi6l5I5GLi6l2kG.png';
    } elseif ($_G['companyname'] == '领泽新营销') {
        $_G['companylogo'] = 'http://cdn.zuanla.net/images/global/EWDxz8owYxcxDZvlALjVs5sajZCaSS.jpg/99.jpg';
    } elseif ($_G['companyname'] == '金策软件') {
        $_G['companylogo'] = 'http://img2019.yixuansoft.cn/images/global/w0cCuxMZOCNUf0y6Az755A9n6Nk5fc.png';
    } else {
        $_G['companylogo'] = 'http://img2019.yixuansoft.cn/images/global/k89oZI8oiC5FAiwsacb8GeGcIcCIgo.png';
    }
}

unset($server_confg);

$_G['isfounder'] = 1;

//$system['s']['banklisturl']=$server_confg['banklisturl']; //OA对话网址
//$system['s']['qrserverurl']=$server_confg['qrserverurl']; //成交提醒
//$system['s']['auth_server']=$server_confg['auth_server']; //小程序授权
//$system['s']['ishongbao']=$server_confg['ishongbao']; //红包总模块
//$system['s']['isfenxiao']=$server_confg['isfenxiao']; //分销功能
//$system['s']['iszhuanf']=$server_confg['iszhuanf ']; //a转发红包
//$system['s']['isyaoyiyao']=$server_confg['isyaoyiyao ']; //摇一摇
//$system['s']['isticheng']=$server_confg['isticheng']; //员工提成
//$system['s']['isyuedu']=$server_confg['isyuedu']; //阅读红包
//$system['s']['isicon']=$server_confg['isicon ']; //点亮星星
//$system['s']['isfanpai']=$server_confg['isfanpai ']; //翻牌
//$system['s']['isurl']=$server_confg['isurl']; //分享域名
//$system['s']['isshare']=$server_confg['isshare']; //分享信息
//$system['s']['isjpeg']=$server_confg['isjpeg ']; //活动结束jpeg
//$system['s']['istouxiang']=$server_confg['istouxiang ']; //真实头像
//$system['s']['isfujintuan']=$server_confg['isfujintuan']; //附近团
//$system['s']['isbanquan']=$server_confg['isbanquan']; //版权信息
//$system['s']['isweixinxserver']=$server_confg['isweixinxserver']; //微信授权登录
//$system['s']['isheadimg']=$server_confg['isheadimg']; //头像
//$system['s']['istime']=$server_confg['istime ']; //倒计时
//$system['s']['isbutton']=$server_confg['isbutton ']; //底部按扭
//$system['s']['isdanmu']=$server_confg['isdanmu']; //弹幕
//$system['s']['istubiao']=$server_confg['istubiao ']; //海报图标


//临时测试数据
//$_G['member']['uid'] = 1353;
//$_G['mpid'] = 22;//平台表id
//$_G['theme']['softkey'] = 'BwMMSizbi';
//$_G['siteroot'] = "http://d3.dev.yixuan.net/";
//!defined('MODULE_NAME') && define('MODULE_NAME', 'mq19010');